#include "Tileset.h"

namespace GenBen {

Tileset::Tileset()
	: graphics(NULL), texture(NULL), tile_size(0, 0)
{
}

Tileset::Tileset(Graphics* graphics, const string& filename, const VectorI& tile_size)
	: graphics(graphics), texture(NULL), tile_size(tile_size)
{
	texture = graphics->loadTexture(filename);
	
	ASSERT(texture->getWidth() % tile_size.x == 0);
	ASSERT(texture->getHeight() % tile_size.y == 0);

	width = texture->getWidth() / tile_size.x;
	height = texture->getHeight() / tile_size.y;
}

Tileset::~Tileset()
{
	if(texture) delete texture;
}

Texture* Tileset::getTexture() const
{
	return texture;
}

RectI Tileset::getSourceRect(uint tile) const
{
	uint x = tile % width;
	uint y = tile / width;

	return RectI(x * tile_size.x, y * tile_size.y, (x+1) * tile_size.x, (y+1) * tile_size.y);
}

} // namespace